<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <script src="/js/spritejs.js"></script>
  <script src="//lib.baomitu.com/dat-gui/0.7.2/dat.gui.min.js"></script>
  <style>
    #adaptive {
      width: 100%;
      padding-bottom: 50%;
      background: #eee;
    }
  </style>
</head>
<body>
  <div id="adaptive"></div>
  <script>
    const {Scene, Sprite, Label, Path} = spritejs;
    const container = document.getElementById('adaptive');
    const scene = new Scene({
      container,
      width: 1200,
      height: 600,
      // contextType: '2d',
    });
    const layer = scene.layer();

    const s1 = new Sprite();
    s1.attr({
      anchor: [0.5, 0.5],
      pos: [770, 300],
      size: [300, 300],
      rotate: 45,
      bgcolor: '#3c7',
    });

    layer.append(s1);

    s1.addEventListener('mouseenter', (evt) => {
      s1.attr('border', [4, 'blue']);
    });
    s1.addEventListener('mouseleave', (evt) => {
      s1.attr('border', [0, '']);
    });

    const anchorCross = new Path('M-10,0H10M0,-10V10');
    anchorCross.attr({
      pos: [770, 300],
      strokeColor: 'red',
      rotate: 45,
      lineWidth: 4,
      pointerEvents: 'none',
    });

    layer.append(anchorCross);

    const label = new Label('鼠标位置：');

    label.attr({
      pos: [20, 50],
      font: '24px Arial',
      lineHeight: 56,
    });

    layer.append(label);

    s1.addEventListener('mousemove', (evt) => {
      const {x, y} = evt;
      label.text = `鼠标位置：\n相对于锚点: ${s1.getOffsetPosition(x, y).map(Math.round)}`;
    });
  </script>
</body>
</html>
